package com.framework.sc.graphqldemo.service;

import com.framework.sc.graphqldemo.dao.TripRepository;
import com.framework.sc.graphqldemo.po.Trip;
import io.leangen.graphql.annotations.GraphQLArgument;
import io.leangen.graphql.annotations.GraphQLContext;
import io.leangen.graphql.annotations.GraphQLMutation;
import io.leangen.graphql.annotations.GraphQLQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @authore zyx
 * @date 2018/8/14 10:40
 */
@Service
public class TripService {

    private final TripRepository tripRepository;
    private final GiphyService giphyService;

    public TripService(TripRepository tripRepository, GiphyService giphyService){
        this.tripRepository = tripRepository;
        this.giphyService = giphyService;
    }

    @GraphQLQuery(name = "trips")
    public List<Trip> getTrips(){
        return  tripRepository.findAll();
    }

    @GraphQLQuery(name = "trip")
    public Optional<Trip>  getTripById(@GraphQLArgument(name = "id") String id ){
        return tripRepository.findById(id);
    }

    @GraphQLMutation(name = "saveTrip")
    public Trip saveTrip(Trip trip){
        return tripRepository.save(trip);
    }

    @GraphQLMutation(name = "deleteTrip")
    public void deleteTrip(String id){
        tripRepository.deleteById(id);
    }

    @GraphQLQuery(name = "isCool")
    public boolean isCool(@GraphQLContext Trip trip) {
        return trip.getTitle().equals("test");
    }

    @GraphQLQuery(name = "giphyUrl")
    public String getGiphyUrl(@GraphQLContext Trip trip) {
        return giphyService.getGiphyUrl(trip.getTitle());
    }
}
